import React, { Component } from 'react'
import { Form, Button, Switch, Radio } from 'antd'
import { messages } from "share/common";
import Condition from 'components/condition'
import Priority from "components/template/priority";
import constant from "../constant";
import SaveHoc from "./save-hoc";

class CashRule extends Component {

  state = {
    priority: null,
    saveLoading: false
  };

  componentWillMount() {
    const { record } = this.props.params;
    this.setState({
      priority: record.loanOptionId ? {
        levelOrgName: record.levelOrgName,
        levelOrgId: record.levelOrgId,
        distribution: record.isGlobal ? null : {
          companies: record.fmLoanOptionDistList.map(item => { return { name: item.levelOrgName, id: item.levelOrgId } })
        }
      } : null
    })
  };

  options = [
    { label: messages('finance-1.key244')/*启用temp*/, value: 'true' },
    { label: messages('finance-1.key266')/*不启用*/, value: 'false' }
  ];

  handleSave = () => {
    this.props.form.validateFieldsAndScroll((err, value) => {
      if (!err) {
        this.props.handleSave('saveCashRule', value, 'fmLoanOptionDistList');
      }
    });
  };

  handlePriority = value => {
    this.setState({ priority: value });
  };

  render() {
    const { getFieldDecorator } = this.props.form;
    const { saveLoading, priority } = this.state;
    const { close } = this.props, { record } = this.props.params;
    const { formItemLayout } = constant;
    return (
      <div>
        <Form className={'new-form'}>
          <Condition>
            <Condition.If value={record.onlyRead}>
              <Form.Item
                label={messages('finance-1.key368')/*收款账户必填*/}
                {...formItemLayout}
                extra={messages('finance-1.key369')/*借款单提交时，校验借款行的收款人账户是否填写*/}
                required
              >
                <div>{messages('finance-1.key266')/*不启用*/}</div>
              </Form.Item>
            </Condition.If>
            <Condition.Else>
              <Form.Item
                label={messages('common.column.status')/*状态*/}
                {...formItemLayout}
              >
                {getFieldDecorator("isEnable", {
                  rules: [{ required: true }],
                  valuePropName: "checked",
                  initialValue: record.hasOwnProperty('isEnable') ? record.isEnable : 'true'
                })(
                  <Switch>{messages('finance-1.key244')/*启用temp*/}</Switch>
                )}
              </Form.Item>
              <Priority
                disabledTop={record.hasOwnProperty('loanOptionId')}
                top='SET_OF_BOOK'
                onChange={this.handlePriority}
                formItemLayout={formItemLayout}
                defaultValue={priority}
                single={false}
                showCompany={false}
              />
              <h3 className='sub-title'>{messages('finance-1.key370')/*借款提交配置*/}</h3>
              <Form.Item
                label={messages('finance-1.key368')/*收款账户必填*/}
                {...formItemLayout}
                extra={messages('finance-1.key369')/*借款单提交时，校验借款行的收款人账户是否填写*/}
              >
                {getFieldDecorator("option1", {
                  rules: [{ required: true }],
                  initialValue: record.hasOwnProperty('option1') ? record.option1 : 'true'
                })(
                  <Radio.Group options={this.options}/>
                )}
              </Form.Item>
            </Condition.Else>
          </Condition>
        </Form>
        <div className='footer-operate'>
          <Condition.If value={!record.onlyRead}>
            <Button type='primary' onClick={this.handleSave} loading={saveLoading}>{messages('common.ok')/*确定*/}</Button>
          </Condition.If>
          <Button onClick={() => close()}>{record.onlyRead ? messages('common.back')/*返回*/ : messages('common.cancel')/*取消*/}</Button>
        </div>
      </div>
    )
  }

}

export default Form.create()(SaveHoc(CashRule));
